package com.asion.qkan.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asion.qkan.R;
import com.asion.qkan.adapters.TuijianItemAdapter;
import com.asion.qkan.entry.RecommendEntry;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gino on 16-10-13.
 */

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> {
    public static final int LUNBO = 0;
    private Context context;
    private List<Object> list;
    private int type = 0;
    private List<String> titleList;
    private ViewHolder viewpagerHolder;
    private ViewHolder tuijianHolder;

    private RecViewpagerAdapter.OnViewpagerItemListener onviewpager;
    public CategoryAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
        titleList = new ArrayList<>();
    }

    public TuijianItemAdapter.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(TuijianItemAdapter.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0) return LUNBO;
        return 1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        viewpagerHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.viewpager_item, parent, false));
        tuijianHolder = new ViewHolder( LayoutInflater.from(context).inflate(R.layout.tuijian_item, parent, false));
        if (viewType == LUNBO) {
            return viewpagerHolder;
        }else{
            return tuijianHolder;
        }
    }
    public void setonViewpagerItemListener(RecViewpagerAdapter.OnViewpagerItemListener onviewpager){
        this.onviewpager = onviewpager;
    }
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
            Object o = list.get(position);
            if (position==0) {
                if (holder.viewPager.getAdapter() == null) {
                    List<RecommendEntry.DataBean.TopicsBean> topics = ((RecommendEntry.DataBean) o).getTopics();
                    RecViewpagerAdapter adapter = new RecViewpagerAdapter(context, topics);
                    holder.viewPager.setAdapter(adapter);
                    adapter.setOnViewpagerItemListener(onviewpager);
                    for (RecommendEntry.DataBean.TopicsBean bean : topics) {
                        ImageView imageView = new ImageView(context);
                        imageView.setImageResource(R.drawable.point);
                        int v = (int) context.getResources().getDimension(R.dimen.point_padding);
                        imageView.setPadding(v, v, v, v);
                        holder.linear.addView(imageView);
                    }
                    holder.viewPager.addOnPageChangeListener(holder);
                    holder.viewPager.setCurrentItem(1);//将第一张轮播图片放在第一位
                    holder.viewPager.post(new Runnable() {
                        @Override
                        public void run() {
                            ViewCompat.setTranslationX(holder.point, holder.linear.getLeft());
                        }
                    });
                    holder.handler.obtainMessage(0, 1, 0).sendToTarget();
                }
            }else{
                setDataType(holder, (RecommendEntry.DataBean) o,position);
            }
    }

    private void setDataType(ViewHolder holder, RecommendEntry.DataBean o, int pos) {
        holder.tuijianTitle.setText(titleList.get(pos));
        List<RecommendEntry.DataBean.TopicsBean> topics = o.getTopics();
        holder.itemRecycler.setLayoutManager(new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL));
        holder.itemRecycler.setItemAnimator(new DefaultItemAnimator());
        TuijianItemAdapter adapter = new TuijianItemAdapter(topics, context,onItemClickListener);
        holder.itemRecycler.setAdapter(adapter);
    }

    public void addData(Object o, int position,String title) {
        titleList.add(title);
        list.add(position, o);
        notifyDataSetChanged();
    }

    public void removeAll(){
        list.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener, Handler.Callback {
        private RecyclerView itemRecycler;
        private  TextView tuijianTitle;
        private ViewPager viewPager;
        private ImageView point;
        private LinearLayout linear;

        private Handler handler = new Handler(this);

        public ViewHolder(View itemView) {
            super(itemView);
            linear = (LinearLayout) itemView.findViewById(R.id.focus_points);
            point = (ImageView) itemView.findViewById(R.id.focus_point);
            viewPager = (ViewPager) itemView.findViewById(R.id.viewpager_item_viewpager);
            tuijianTitle = ((TextView) itemView.findViewById(R.id.tuijian_title));
            itemRecycler = ((RecyclerView) itemView.findViewById(R.id.item_recycler));
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = linear.getLeft() + (position - 1 + positionOffset) * point.getWidth();
            value = Math.max(value, linear.getLeft());
            value = Math.min(value, linear.getRight() - point.getLeft());
            ViewCompat.setTranslationX(point, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = viewPager.getAdapter();
            if (position == 0) {
                viewPager.setCurrentItem(adapter.getCount() - 2, false);
            } else if (position == adapter.getCount() - 1) {
                viewPager.setCurrentItem(1, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int arg1 = msg.arg1;
                    if (arg1 > viewPager.getAdapter().getCount() - 2) {
                        arg1 = 1;
                    }
                    viewPager.setCurrentItem(arg1, arg1 != 1);
                    Message message = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message, 5000);
                    break;
            }
            return true;
        }
    }

}
